<!--

Saherace - Rent a Car

This page shows all rentals of a customer to rate the car of the rental.

-->

<!-- Include the soap client connection and the header area of the website -->
<?php include('includes/header.php'); ?>

<?php

if($_SESSION['type']  == 'login') {
	//Rate with login
	if(isset($_POST['customer_id']))
		$_SESSION['customer_id']	= $_POST['customer_id'];
	if(isset($_POST['customer_pwd']))
		$_SESSION['customer_pwd']	= $_POST['customer_pwd'];

	//Check the inserted login data
	$params = array('customer_id' => $_SESSION['customer_id'], 'password' => $_SESSION['customer_pwd']);
	$rentalLogin = $client->rentalLogin($params);
	
	if(!$rentalLogin->return) {
		//If login wasn't successful, go back to rate_login.php
		echo '<script>'.
				'window.location.replace("rate_login.php?login=false"); '.
			 '</script>';
		exit;
		$_SESSION['login'] = 'false';
		unset($_SESSION['customer_id']);
	} else {
		//If login was successful
		$_SESSION['login'] = 'true';
	}
}

//Update the rate, if a car was rated
if(isset($_POST['rental_id'])) {
	$rate_params = array('rental_id' => $_POST['rental_id'], 'rate' => $_POST['rate']);
	$client->rateCar($rate_params);
}

//Get the rentals of the customer
$customer_id = array('customer_id' => $_SESSION['customer_id']);
$getUsersRentals = $client->getUsersRentals($customer_id);

//Get the customer details
$customer_id = array('customer_id' => $_SESSION['customer_id']);
$getCustomerDetails = $client->getCustomerDetails($customer_id);
?>

<!-- List all important customer details -->
<ul data-role="listview" data-inset="true">
	<li data-role="list-divider">Bewertung</li>
	<li>
		<table>
			<tr>
				<td width="150">Kundennummer:</td>
				<td><?php echo $getCustomerDetails->return->customer_id; ?></td>
			</tr>
			<tr>
				<td width="150">Name:</td>
				<td><?php echo $getCustomerDetails->return->surname . " " . $getCustomerDetails->return->name ; ?></td>
			</tr>
			<tr>
				<td>E-Mail:</td>
				<td><?php echo $getCustomerDetails->return->email; ?></td>
			</tr>
			<tr>
				<td>Telefon:</td>
				<td><?php echo $getCustomerDetails->return->phone; ?></td>
			</tr>
		</table>
	</li>
	
	<!-- List all rentals of the customer -->
	<?php 
	if (is_object($getUsersRentals) && (count(get_object_vars($getUsersRentals)) > 0)) {
		//If its more than one rental
		if (sizeof($getUsersRentals->return) > 1) {
			for($i = 0; $i<sizeof($getUsersRentals->return); $i++){
				echo '<li>';
				echo '<img src="data:image/jpeg;base64,'.base64_encode($getUsersRentals->return[$i]->car->photo).'" />';			
				echo '<h3>'.$getUsersRentals->return[$i]->car->brand.' '. $getUsersRentals->return[$i]->car->carModel.'</h3>';
				$date = new DateTime($getUsersRentals->return[$i]->start_date);
				echo '<p>Gemietet am: <strong>'.$date->format("d.m.Y H:i").' Uhr</strong></p>';
				echo '<form action="rate.php" method="POST">';
				echo '<select name="rate" data-mini="true" onChange="this.form.submit()">';
				echo '<option value="1" ';
				if($getUsersRentals->return[$i]->raiting == 1) echo "selected";
				echo ' >1 Stern</option>';
				echo '<option value="2" ';
				if($getUsersRentals->return[$i]->raiting == 2) echo "selected";
				echo ' >2 Sterne</option>';
				echo '<option value="3" ';
				if($getUsersRentals->return[$i]->raiting == 3) echo "selected";
				echo ' >3 Sterne</option>';
				echo '<option value="4" ';
				if($getUsersRentals->return[$i]->raiting == 4) echo "selected";
				echo ' >4 Sterne</option>';
				echo '<option value="5" ';
				if($getUsersRentals->return[$i]->raiting == 5) echo "selected";
				echo ' >5 Sterne</option>';
				echo '</select>';
				echo '<input type="hidden" name="rental_id" value="'.$getUsersRentals->return[$i]->rental_id.'" />';
				echo '</form>';
				echo '</li>';
			}
		//If its only one rental
		} else {
			echo '<li>';
			echo '<img src="data:image/jpeg;base64,'.base64_encode($getUsersRentals->return->car->photo).'" />';			
			echo '<h3>'.$getUsersRentals->return->car->brand.' '. $getUsersRentals->return->car->carModel.'</h3>';
			$date = new DateTime($getUsersRentals->return->start_date);
			echo '<p>Gemietet am: <strong>'.$date->format("d.m.Y H:i").' Uhr</strong></p>';
			echo '<form action="rate.php" method="POST">';
			echo '<select name="rate" data-mini="true" onChange="this.form.submit()">';
			echo '<option value="1" ';
			if($getUsersRentals->return->raiting == 1) echo 'selected';
			echo ' >1 Stern</option>';
			echo '<option value="2" ';
			if($getUsersRentals->return->raiting == 2) echo 'selected';
			echo ' >2 Sterne</option>';
			echo '<option value="3" ';
			if($getUsersRentals->return->raiting == 3) echo 'selected';
			echo ' >3 Sterne</option>';
			echo '<option value="4" ';
			if($getUsersRentals->return->raiting == 4) echo 'selected';
			echo ' >4 Sterne</option>';
			echo '<option value="5" ';
			if($getUsersRentals->return->raiting == 5) echo 'selected';
			echo ' >5 Sterne</option>';
			echo '</select>';
			echo '<input type="hidden" name="rental_id" value="'.$getUsersRentals->return->rental_id.'" />';
			echo '</form>';
			echo '</li>';
		}
		//If theres no rental
	} else {
		echo '<li>Sie haben noch kein Fahrzeug gemietet.</li>';
	}
?>
</ul>

<!-- Include the footer area of the website -->
<?php include('includes/footer.php'); ?>
